package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.weiqiao.mall.domain.EquityCardEntity;
import com.weiqiao.mall.repository.IEquityCardRepository;
import com.weiqiao.mall.service.IEquityCardService;
import com.weiqiao.utils.DateUtil;
import com.weiqiao.utils.StringUtils;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("EquityCardServiceImpl")
public class EquityCardServiceImpl implements IEquityCardService {

    @Autowired
    @Qualifier("EquityCardRepositoryImpl")
    private IEquityCardRepository equityCardRepository;

    @Override
    public List<EquityCardEntity> findAllEquityCard(int company_id, Bson filter, Bson sort) {
        List<String> list = equityCardRepository.findAllEquityCard(company_id, filter, sort);
        List<EquityCardEntity> equityCardEntities = new ArrayList<>();
        for (String str : list) {
            EquityCardEntity entity = new Gson().fromJson(str, EquityCardEntity.class);
            Boolean time_flag = false;
            if (StringUtils.isInteger(entity.getTime_state()) && 2 == entity.getTime_state()
                    && !StringUtils.isBlank(entity.getTime_start()) && !StringUtils.isBlank(entity.getTime_end())) {
                long currentTimeMillis = System.currentTimeMillis();
                time_flag = currentTimeMillis >= Long.valueOf(entity.getTime_start()) && currentTimeMillis <= Long.valueOf(entity.getTime_end());
            }
            if (!time_flag) {
                continue;
            }
            if (!StringUtil.isBlank(entity.getCreate_time())) {
                entity.setCreate_time(DateUtil.timestampToString(entity.getCreate_time()));
            }
            equityCardEntities.add(entity);
        }
        return equityCardEntities;
    }
}
